package com.hebang.stcommunityapp.fragments;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.hebang.stcommunityapp.LoginActivity;
import com.hebang.stcommunityapp.R;
import com.hebang.stcommunityapp.activitys.DeviceManagActivity;
import com.hebang.stcommunityapp.activitys.MainActivity;
import com.hebang.stcommunityapp.activitys.WorkOrderManageActivity;
import com.hebang.stcommunityapp.adapters.SyListdapter;
import com.hebang.stcommunityapp.beans.SyItemBean;
import com.hebang.stcommunityapp.databinding.SyLayoutBinding;

import java.util.ArrayList;


/**
 * Created by 任晋超 on 2017/10/25.
 * Fragment
 */


public class SyFragment1 extends Fragment implements View.OnClickListener {
    View view;
    SyLayoutBinding binding;
    private ArrayList<Integer> localImages = new ArrayList<Integer>();
    private ArrayList<SyItemBean> list = new ArrayList<>();
    //定义网络图片控件。
    SimpleDraweeView draweeView;
    SyListdapter adapter;
    RecyclerView recyclerview;
    Intent intent;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        binding = DataBindingUtil.inflate(inflater, R.layout.sy_layout, container, false);
//        ksViewModel = new ViewModelProvider(this).get(KsViewModel.class);
        view = binding.getRoot();
        initView();
        return view;
    }

    private void initView() {
        localImages.add(R.mipmap.banner_deful);
        setdata();

        SyItemBean syItemBean1=new SyItemBean("地图管理",R.mipmap.ditu);
        SyItemBean syItemBean2=new SyItemBean("设备管理",R.mipmap.shebei);
        SyItemBean syItemBean3=new SyItemBean("工单管理",R.mipmap.gongdan);
        SyItemBean syItemBean4=new SyItemBean("告警管理",R.mipmap.gaojing);
        SyItemBean syItemBean5=new SyItemBean("住户管理",R.mipmap.zhuhu);
        SyItemBean syItemBean6=new SyItemBean("访客管理",R.mipmap.fangke);
        SyItemBean syItemBean7=new SyItemBean("车辆管理",R.mipmap.cheliang);
        SyItemBean syItemBean8=new SyItemBean("能耗管理",R.mipmap.haoneng);
        SyItemBean syItemBean9=new SyItemBean("疫情防控",R.mipmap.yiqing);
        list.add(syItemBean1);
        list.add(syItemBean2);
        list.add(syItemBean3);
        list.add(syItemBean4);
        list.add(syItemBean5);
        list.add(syItemBean6);
        list.add(syItemBean7);
        list.add(syItemBean8);
        list.add(syItemBean9);

        initRecycler();
    }


    private void initRecycler(){
        if (adapter == null) {
            recyclerview = binding.statutefragmentRecyclerview;
            GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
            recyclerview.setLayoutManager(layoutManager);
            adapter = new SyListdapter(list);
            recyclerview.setAdapter(adapter);
            adapter.setOnItemClickListener(new SyListdapter.OnItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    switch (position){
                        case 0:

                            break;
                        case 1:
                             intent=new Intent(getActivity(), DeviceManagActivity.class);
                            startActivity(intent);
                            break;
                        case 2:
                            intent=new Intent(getActivity(), WorkOrderManageActivity.class);
                            startActivity(intent);
                            break;
                        case 3:
                            intent=new Intent(getActivity(), DeviceManagActivity.class);
                            startActivity(intent);
                            break;


                    }

                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.gongchenglist_liner:
////                Intent intent = new Intent(getActivity(), ProjectListActivity.class);
////                startActivity(intent);
//                break;
        }
    }


    public class LocalImageHolderView implements Holder<Integer> {

        @Override
        public View createView(Context context) {
            //实例化网络图片控件draweeView。也可以通过finfviewbyid的方式绑定已经在XML文件中写好的布局
            draweeView = new SimpleDraweeView(context);
            //设置图片在控件中为铺满展示。
            draweeView.setScaleType(ImageView.ScaleType.FIT_XY);


            return draweeView;
        }

        @Override
        public void UpdateUI(final Context context, int position, final Integer data) {

            //draweeView设置图片路径的时候必须为Uri格式。
//            Uri uri = Uri.parse(data);
            Uri uri = new Uri.Builder().scheme("res").path(String.valueOf(data)).build();
            //实例化控件管理者
            GenericDraweeHierarchyBuilder builder =
                    new GenericDraweeHierarchyBuilder(context.getResources());
            //通过GenericDraweeHierarchyBuilder控件管理者的build方法创建控件状态
            GenericDraweeHierarchy hierarchy = builder
//                    .setRoundingParams(RoundingParams.fromCornersRadius(20))
//                    .setFailureImage(R.mipmap.default_no_pic_vedio)//设置加载失败时显示的图片
//                    .setPlaceholderImage(R.mipmap.wait70px)//设施加载中显示的占位图片
                    .build();
            //为SimpleDraweeView控件设置较为复杂的行为管理器
            draweeView.setHierarchy(hierarchy);
            //为SimpleDraweeView控件设置图片加载路径
            draweeView.setImageURI(uri);
//            draweeView.setImageURI(PIC_HOST+data.getTpdz());

        }
    }

    public void setdata() {
        //生成convenientBanner控件的每一页。总页数由localImages的集合长度决定。
        binding.convenientBanner.setPages(
                new CBViewHolderCreator<LocalImageHolderView>() {
                    @Override
                    public LocalImageHolderView createHolder() {
                        return new LocalImageHolderView();
                    }
                }, localImages);
        //开始轮播，设置轮播时间间隔
        binding.convenientBanner.startTurning(3000);


    }
}




